#include "..\\FilesRefactor\\FileMachine.h"
#include "..\FilerFrogShell\ExternalFrogOperations.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HANDLE hMutex;
	DWORD  dwReturn;
	bool   fGotMutex;
	wchar_t szAppname[_MAX_PATH];

	wstring filePath = GetCommandLine();

	GetModuleFileName(
		hInstance,
		szAppname,
		sizeof(szAppname));

	FileMachine fm(NULL, szAppname);
	wstring fName;
	fm.NamePlusExt(fName);

	hMutex = CreateMutex(
		NULL,                 // no security attribute
		FALSE,                // don't acquire initial ownership
		fName.c_str());             // use base name as mutex name

	dwReturn = WaitForSingleObject(
		hMutex,               // handle to wait for
		1000);                // timeout in mS

	fGotMutex = (dwReturn == WAIT_OBJECT_0) || (dwReturn == WAIT_ABANDONED);
	
	int count = 0;
	int j=0;

	int length = (int)filePath.length();

	while (count < 3)
	{
		while (j < length && filePath.at(j) != L'"')
		{
			j++;
		}
		count++;
		j++;
	}
	
	wchar_t path[MAX_PATH];
	
	int i=0;

	while (j < length && filePath.at(j) != L'"')
	{
		path[i] = filePath.at(j);
		i++;
		j++;
	}

	path[i] = L'\0';
	
	if (fGotMutex)
	{
		ExternalFrogOperations externalFrogOperations;
		
		wstring newPath = path;
		externalFrogOperations.ExternalDecryptFileDialog(newPath);

		ReleaseMutex(hMutex);
	}

	CloseHandle(hMutex);

}